package com.findfuture.demo.controller;

import com.findfuture.common.util.StringUtil;
import com.findfuture.demo.model.Albums;
import com.findfuture.demo.model.Catalog;
import com.findfuture.demo.service.IAlbumService;
import com.findfuture.demo.service.ICatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Alex
 * Date: 11-11-19
 * Time: 下午9:20
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/album")
@SessionAttributes("token")
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    @Autowired
    private ICatalogService catalogService;

    @RequestMapping
    public String list(ModelMap modelMap) throws Exception {
        modelMap.addAttribute("token", StringUtil.uuid());
        modelMap.addAttribute("albums", albumService.getAlbumsList());
        return "/album/albums";
    }

    @RequestMapping("addAction")
    public String addAction(ModelMap modelMap, String name, int catalogId, String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken))
            try {
                Albums albums = new Albums();
                albums.setName(name);
                Catalog catalog = catalogService.getCatalogById(catalogId);
                albums.setCatalog(catalog);
                albumService.addAlbum(albums);
                modelMap.addAttribute("success", "添加成功！");
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.addAttribute("error", "添加失败！");
            }
        return "forward:/catalog";
    }

    @RequestMapping("updateAction")
    public String updateAction(ModelMap modelMap, String name, int id, String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken))
            try {
                albumService.updateAlbumName(id, name);
                modelMap.addAttribute("success", "相册修改成功！");
            } catch (Exception e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                modelMap.addAttribute("error", "相册修改失败！");
            }
        return "forward:/catalog";
    }

    @RequestMapping("/deleteAlbum/{albumId}/{token}")
    public String deleteAlbum(ModelMap modelMap, @PathVariable int albumId,
                              @PathVariable String token, @ModelAttribute("token") String sessionToken) {
        if (token.equals(sessionToken)) {
            try {
                List list = albumService.getAlbumsById(albumId).getImagesList();
                if (list != null && list.size() > 0) {
                    throw new Exception("");
                }
                albumService.deleteAlbum(albumId);
                modelMap.addAttribute("success", "操作成功！");
            } catch (Exception e) {
                e.printStackTrace();
                modelMap.addAttribute("error", "操作失败！");
            }
        }
        return "forward:/catalog";
    }
}
